// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/home/widgets/QGTypeSwitchWidget.dart';
import 'package:junyeyishu/ui/sale/widgets/QGSwitchWidget.dart';

import 'widget/QGMineOrderCell.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGListState.dart';
import 'package:junyeyishu/widgets/QGNavigationBar.dart';

class QGMineHomeOrderPage extends StatefulWidget {
  const QGMineHomeOrderPage({super.key});
  @override
  State<QGMineHomeOrderPage> createState() => _QGMineHomeOrderPageState();
}

class _QGMineHomeOrderPageState extends QGListState<QGMineHomeOrderPage> {
  int type = 0;
int ttype =0;
  Map<String, dynamic>? userInfo;

  String? listURL = url_user_orderList;

  List<String> typeitem=["已买入", "待支付", "已取消"];

  @override
  void initState() {
    super.initState();

    // listURL = url_user_orderList;
    listKey = "list";
    onRefresh();

    Network.get(url_u_getMergedUserInfo).then((res) async {
      if (!res.success) {
        toast(res.msg);

        //处理token失效的情形
        if (!User.isLogin) {
          await Future.delayed(const Duration(seconds: 2));
          Navigator.of(context).pushNamed(PageLogin).then((value) {
            if (!User.isLogin) {
              eventBus.fire(EventMainTabSelectIndex(index: 0));
            }
          });
        }
        return;
      }

      userInfo = res.data?["user_info"];

      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  listParam() {
    if(ttype==0){
      switch (type) {
            case 0:
              return {"type": 3};
            case 1:
              return {"type": 1};
            case 2:
              return {"type": 2};
            default:
              break;
          }
    }else if(ttype==1){
      switch (type) {
        case 0:
          return {"tab_id": 1};
        case 1:
          return {"tab_id": 2};
        case 2:
          return {"tab_id": 3};
        case 3:
          return {"tab_id": 4};
        case 4:
          return {"tab_id": 5};
        default:
          break;
      }
    }
    else if(ttype==2||ttype==3){
      switch (type) {
      case 0:
        return {"order_status": 4};
      case 1:
        return {"order_status": 6};
      case 2:
        return {"order_status": 2};
      case 3:
        return {"order_status": 6};
      default:
        break;
    }
    }

   
    return {};
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // backgroundColor: Color(0xFFF8F8F6),
        body: Column(
          children: [
            buildHeader(),
            buildList(),
          ],
        ));
  }

  buildHeader() {
    return Container(
      color: Color(0xff1d3f41),
      child: Column(children: [
        QGNavigationBar(title: "我的订单"),
        
        Container(
           color: Color(0xff0C1012),
          child: SingleChildScrollView(
          child:
          Column(
            children: [
              QGTypeSwitchWidget(
              alignment: MainAxisAlignment.center,
              current: ttype,
              items: ["一级订单", "寄售订单", "求购订单",'委托订单','转赠订单'],
              underline: true,
              fontSize: 13,
              maxFontSize: 14,
              onSwitch: (index){
                  print(index);
                  ttype = index;
                  if(index==0){
                   listURL= url_user_orderList;
                    typeitem = ["已买入", "待支付", "已取消"];
                    type=0;
                     onRefresh();
                      // Navigator.of(context).pushNamed(PageMyHomeOrder);
                  }else if(index == 1){
                    listURL = url_market_orderList;
                    typeitem = ["已买入", "待支付",'进行中', "已取消",'已寄售'];
                    type=0;
                     onRefresh();
                      // Navigator.of(context).pushNamed(PageMyOrder);
                  }else if(index == 2){
                    listURL = url_ask_getMyOrderList;
                    typeitem = ["求购中", "已成交", "已取消", "已卖出"];
                    type=0;
                      onRefresh();
                     list = null;
                      // Navigator.of(context).pushNamed(PageMyPurchaseOrder);
                  }else if(index == 3){
                    listURL = url_entrust_getMyOrderList;
                    typeitem = ["委托中", "已取消",'已支付' ,"已完成"];
                    type=0;
                      onRefresh();
                      list = null;
                  }else if(index == 4){
                    listURL = url_user_getMyPresentList;

                    onRefresh();
                    //Navigator.of(context).pushNamed(PageNFTSendRecord);
                  }
                  setState(() {
                    
                  });
              }),

                    if(ttype != 4)
                    Container(
                      height: 40.rpx,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(30.rpx),
                            color: Color(0XFF232A32),
                          ),
                    margin: EdgeInsets.symmetric(vertical: 10.rpx,horizontal: 15.rpx),
                    padding: EdgeInsets.only(right: 10.rpx,top: 5,bottom: 5),
                    child: QGSwitchWidget(
                        types:1,
                        index: type,
                        items:typeitem,
                        onSwitch: (index) {
                          type = index;
                          list = null;
                          setState(() {});
                          onRefresh();
                        })
                   ,
                  )
             
              
         
          

              // QGTypeSwitchWidget(
              // alignment: MainAxisAlignment.center,
              // current: type,
              // items: ["已买入", "待支付", "已取消"],
              // underline: true,
              // fontSize: 13,
              // maxFontSize: 14,
              // onSwitch: onSwitch),

        //       Container(
        //   child: QGSwitchWidget(
        //   types:1,
        //   index: type,
        //   items: ["全部",'已买入','待付款'],
        //   onSwitch: (index) {
        //     type = index;
        //     list = null;
        //      setState(() {});
        //     // onRefresh();
        //   }),
        // ),
            ],
          )
           
        ),
        ),

      ]),
    );
  }

  onSwitch(index) {
    type = index;
    list = null;
    setState(() {});
    onRefresh();
  }

  @override
  itemBuilder(context, index, data) {
    data ??= {};
    data["types"] = data["type"];
    data["type"] = type;
    data["ttype"] = ttype;
    

    data["status"] = type;
    data["tab"] = 1;

    return GestureDetector(
        onTap: () {
          if(ttype==0){
            if (type == 1) {
                onPay(data["out_trade_no"]);
            }
          }else if(ttype==1){
           // if (type == 0) {
               Navigator.of(context)
              .pushNamed(PageMyOrderDetail, arguments: data)
              .then((value) {
            onRefresh();
          });
         //   }
          }
          

         
        },
        child: QGMineOrderCell(data: data));
  }

  onPay(no) {
    var param = {};
    param['out_trade_no'] = no;
    Map<String, dynamic> stringMap = param.cast<String, dynamic>();

    var url;
    if(ttype==0){
       url = url_huifu_repay;
    }else if(ttype==1){
       url = url_market_rePayOrder;
    }

    Network.post(url, data: stringMap, context: context)
        .then((res) async {
      if (!res.success) {
        toast(res.msg);
        onRefresh();
        return;
      }

      var url = res.data["pay_url"];
      if (url is String && !url.startsWith("http")) {
        url = url;
      }

      if (url is! String || !url.startsWith("http")) {
        toast("数据解析失败");
        return;
      }

      Navigator.of(context).pushNamed(PageQGPayWebView, arguments: url);
    });
  }
}
